package com.app.android.coliy.ui.activity;


import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.app.android.coliy.R;
import com.app.android.coliy.bean.UploadFileInfo;
import com.app.android.coliy.dialog.CommList_Dialog;
import com.app.android.coliy.event.Event_ChangeLanguge;
import com.app.android.coliy.interface_.CommCallBack;
import com.app.android.coliy.manager.LangugeManager;
import com.app.android.coliy.tool.Log;
import com.app.android.coliy.util.BitmapUtil;
import com.app.android.coliy.util.GlideCacheEngine;
import com.app.android.coliy.util.GlideEngine;
import com.app.android.coliy.util.Util;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    public void changeAppLanguage() {
        // 本地语言设置
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = LangugeManager.getCurrentLanguge();
        res.updateConfiguration(conf, dm);
    }

    //切换语言
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event_ChangeLanguge event) {
        Log.i("收到刷新语言");
        changeAppLanguage();
        recreate();//刷新界面
    }

    public Context mContext;
    public String title = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        changeAppLanguage();
        mContext = this;
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   //禁用横屏

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);
//        MobclickAgent.onPageStart(TextUtils.isEmpty(title) ? getClass().getSimpleName() : title);
    }


    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
//        MobclickAgent.onPageEnd(TextUtils.isEmpty(title) ? getClass().getSimpleName() : title);
    }

    //设置标题
    public void setTitle(String title) {
        this.title = title;
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        if (tv_title != null) {
            tv_title.setText(title);
        }
    }

    //设置左侧返回按钮
    public void setLeftImgClickListener() {
        ImageView public_title_left_img = (ImageView) findViewById(R.id.public_title_left_img);
        public_title_left_img.setVisibility(View.VISIBLE);
        public_title_left_img.setOnClickListener(this);
    }

    //设置左侧按钮图片
    public void setLeftImgResouce(int resouce) {
        ImageView public_title_left_img = (ImageView) findViewById(R.id.public_title_left_img);
        public_title_left_img.setImageResource(resouce);
    }

    //设置右侧图片按钮
    public void setRightImgClickListener(int rightImgResouceID) {
        ImageView public_title_right_img = (ImageView) findViewById(R.id.public_title_right_img);
        public_title_right_img.setImageResource(rightImgResouceID);
        public_title_right_img.setVisibility(View.VISIBLE);
        public_title_right_img.setOnClickListener(this);
    }
    //设置右侧图片按钮
    public void setRightImg1ClickListener(int rightImgResouceID) {
        ImageView public_title_right_img1 = (ImageView) findViewById(R.id.public_title_right_img1);
        public_title_right_img1.setImageResource(rightImgResouceID);
        public_title_right_img1.setVisibility(View.VISIBLE);
        public_title_right_img1.setOnClickListener(this);
    }

    //设置右侧文字
    public void setRightTextClickListener(String text) {
        TextView tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_right_text.setText(text);
        tv_right_text.setVisibility(View.VISIBLE);
        tv_right_text.setOnClickListener(this);
    }

    public void setRightTextEnable(boolean isEnable) {
        TextView tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_right_text.setEnabled(isEnable);
        if (isEnable) {
            tv_right_text.setTextColor(getResources().getColor(R.color.mainColor));
        } else {
            tv_right_text.setTextColor(Color.parseColor("#cccccc"));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.public_title_left_img:
               onBackPressed();
                break;
        }
    }



    //---------------------------上传照片逻辑----------------------------------------


    CommCallBack upLoadCallBack;
    int width = 1;          //默认尺寸比例
    int height = 1;
    boolean isCrop = false;  //是否需要裁剪
    boolean isVideo = false;  //传视频还是传照片


    public void upLoadPhoto(boolean isCrop, int width, int height, final CommCallBack callBack) {
        this.isCrop = isCrop;
        this.width = width;
        this.height = height;
        this.isVideo = false;
        upLoadPhoto(isCrop, callBack);
    }

    public void upLoadPhoto(boolean isCrop, final CommCallBack callBack) {
        this.isCrop = isCrop;
        this.upLoadCallBack = callBack;
        this.isVideo = false;
        List<String> list = new ArrayList<>();
        list.add(Util.getString(R.string.comm_camare));
        list.add(Util.getString(R.string.comm_gallery));
        CommList_Dialog dialog = new CommList_Dialog(mContext);
        dialog.setData(list, null, false, true);
        dialog.setCallBack(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                int selectStr = (int) obj;
                if (selectStr == 0) {
                    openCamera();
                } else if (selectStr == 1) {
                    openAlbum();
                }
            }
        });
        dialog.show();
    }


    private void openCamera() {
        PictureSelector.create(this)
                .openCamera(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .loadCacheResourcesCallback(GlideCacheEngine.createCacheEngine())
                .isAndroidQTransform(true)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {

                        if (result.size() > 0) {
                            LocalMedia media = (LocalMedia) result.get(0);

                            File file;

                            if (media.getAndroidQToPath() != null) {
                                file = new File(media.getAndroidQToPath());
                            }
                            else {
                                file = new File(media.getPath());
                            }

                            doUploadImg(file, media.getWidth(), media.getHeight());
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }


    //打开相册
    void openAlbum() {

        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .loadCacheResourcesCallback(GlideCacheEngine.createCacheEngine())
                .selectionMode(PictureConfig.SINGLE)
                .isSingleDirectReturn(true)
                .circleDimmedLayer(true)
                .isAndroidQTransform(true)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {

                        if (result.size() > 0) {
                            LocalMedia media = (LocalMedia) result.get(0);

                            File file;

                            if (media.getAndroidQToPath() != null) {
                                file = new File(media.getAndroidQToPath());
                            }
                            else {
                                file = new File(media.getPath());
                            }

                            doUploadImg(file, media.getWidth(), media.getHeight());
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }


    private void doUploadImg(File tempFile, int width, int height) {
        if (upLoadCallBack != null) {
            String filename = "img_" + System.currentTimeMillis() + ".jpg";
//            File tempFile = Util.compressImageToFile(file, filename);

            if (tempFile != null) {
                Bitmap bitmap = BitmapUtil.getSmallBitmap(tempFile.getAbsolutePath());
                UploadFileInfo fileInfo = new UploadFileInfo();
                fileInfo.width = width;
                fileInfo.height = height;
                fileInfo.file = tempFile;
                fileInfo.bitmap = bitmap;
                upLoadCallBack.onResult(fileInfo);
            }
        }
//        showLoadingDialog("正在上传");
//        try {
//            File tempFile = Util.compressImageToFile(file);
//            final Bitmap bitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
//            API_LoginManager.upload(mContext, Arrays.asList(tempFile), "11.jpg", new OkHttpCallBack() {
//                @Override
//                public void onSuccess(BaseResponce baseResponce) {
//                    StringResponce responce = (StringResponce) baseResponce;
//                    dismissLoadingDialog();
//                    if ("200".equals(baseResponce.code)) {
//                        if (upLoadCallBack != null) {
//                            Log.i("上传成功 url:" + responce.data);
//                            UploadFileInfo fileInfo = new UploadFileInfo();
//                            fileInfo.url = responce.data;
//                            fileInfo.width = bitmap.getWidth();
//                            fileInfo.height = bitmap.getHeight();
//                            upLoadCallBack.onResult(fileInfo);
//                        }
//                    } else {
//                        CommToast.showToast(mContext, baseResponce.msg);
//                    }
//                }
//
//                @Override
//                public void onFailure(BaseResponce baseResponce) {
//                    dismissLoadingDialog();
//                    CommToast.showToast(mContext, "请求失败，请重试");
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }


}
